//页面加载完成设置菜单记录
window.onload = function () {
    console.log(111)
    setMenu()
    if(localStorage.getItem('currentMenu')) {
        let iframeBox = document.getElementById('iframe-box')
        let menu = JSON.parse(localStorage.getItem('menu')) || [];
        let currentMenu = JSON.parse(localStorage.getItem('currentMenu'))
        if(menu.length > 0){
            menu.map(item=>{
                let html = '<iframe id="iframe'+item.id+'" src="'+item.path+'" frameborder="0" width="0" style="height:0" ></iframe>'
                iframeBox.insertAdjacentHTML('beforeend',html)
            })
        }

        setIframe(JSON.parse(localStorage.getItem('currentMenu')), 'change')
    }


}

//切换页面
function handleMenu(data) {
    // alert(data)
    let menu = JSON.parse(localStorage.getItem('menu')) || []

    if(!menu.find(item=>item.path == data.path)){
        menu.push(data)
        setIframe(data,'add')
    }
    localStorage.setItem('menu',JSON.stringify(menu))
    localStorage.setItem('currentMenu',JSON.stringify(data))

    setMenu()
    setIframe(data,'change')
    // window.location.assign(data.path)
    // window.location.href = data.path
}
//设置菜单html
function setMenu(){
    let menu = JSON.parse(localStorage.getItem('menu')) || []
    let currentMenu = JSON.parse(localStorage.getItem('currentMenu'))

    // alert(JSON.stringify(menu),JSON.stringify(currentMenu))
    if(menu && menu.length > 0){
        let html = '';
        menu.map(item=>{
            // item = item
            html = html + '<div class="item">' +
                '<span class="'+(item.path == currentMenu.path ? 'active' : '')+'"></span>' +
                '<a href="javascript:;" onclick=handleMenu('+JSON.stringify(item)+')>'+item.name+'</a>' +
                '<i class="layui-icon" onclick=handleDelMenu('+JSON.stringify(item)+')>&#x1006;</i>' +
                '</div>'
        })
        // alert(html)
        // alert(document.getElementById('route-log'))
        document.getElementById('route-log').innerHTML = html;
    }else{
        document.getElementById('route-log').innerHTML = '';
    }
}
//更新iframe data: 操作的数据 type：1 增加 2 删除
function setIframe(data,type){
    let iframeBox = document.getElementById('iframe-box')

    if(type == 'add'){
        let html = '<iframe id="iframe'+data.id+'" src="'+data.path+'" frameborder="0" width="100%" style="height:calc(100vh - 97px - 15px)" ></iframe>'
        iframeBox.insertAdjacentHTML('beforeend',html)
    }
    if(type == 'del'){
        let delIframeBox = document.getElementById('iframe'+data.id)
        if(delIframeBox){
            delIframeBox.parentNode.removeChild(delIframeBox)
        }
    }
    if(type == 'change'){
        for(var i = 0; i < iframeBox.children.length; i++){
            let childId = iframeBox.children[i].id
            if(childId == 'iframe'+data.id){
                iframeBox.children[i].style.width = "100%";
                iframeBox.children[i].style.height = "calc(100vh - 97px - 15px)";
            }else{
                iframeBox.children[i].style.width = "0";
                iframeBox.children[i].style.height = "0";
            }
        }
    }
}
//删除菜单记录
function handleDelMenu(data){
    let menu = JSON.parse(localStorage.getItem('menu')) || []
    let currentMenu = JSON.parse(localStorage.getItem('currentMenu'))
    if(menu && menu.length > 0){
        if(menu.length == 1){
            // localStorage.removeItem('menu')
            // localStorage.removeItem('currentMenu')
            // setMenu();
            return ;
        }
        let index = menu.findIndex(item=>item.path == data.path)
        menu = menu.filter(item=>item.path != data.path)
        localStorage.setItem('menu',JSON.stringify(menu))
        if(currentMenu.path == data.path){
            localStorage.removeItem('currentMenu')
        }
        setIframe(data,'del')
        handleMenu(menu[index != 0 ? index - 1 : 0])
    }

}


function generateRandomString(length) {
    const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
        const randomIndex = Math.floor(Math.random() * characters.length);
        result += characters[randomIndex];
    }
    return result;
}